#批量pad
import os
import random
from tqdm import tqdm
import shutil
from PIL import Image
import os.path
sameple_path = '/home/xys/CloundShiProjects/traffic_light/trafficlight_classify/test-筛选/error/ng'  #jpg和txt混合路径
sum = 0
for f in os.listdir(sameple_path):
    filedir = os.path.join(sameple_path,f)
    num = 0
    for file in os.listdir(filedir):
        file_d = os.path.join(filedir,file)
        image = Image.open(file_d)
        w, h = image.size
        target = max(w, h)
        image1 = Image.new("RGB", (target, target))
        if h >= w:
            pad_w = int((target - w) / 2)
            image1.paste(image, (pad_w, 0))
            image1.save(file_d)
        else:
            pad_h = int((target - h) / 2)
            image1.paste(image, (0, pad_h))
            image1.save(file_d)
        num+=1
    print(num)
    sum+=num
print(sum)




from PIL import Image
def preprocess(pil_file):
    w, h = pil_file.size
    target = max(w,h)
    image1 = Image.new("RGB", (target, target))
    if h >= w:
        pad_w = int((target - w) / 2)
        image1.paste(pil_file,(pad_w, 0))
    else:
        pad_h = int((target - h) / 2)
        image1.paste(pil_file, (0, pad_h))
    return image1

if __name__ == "__main__":
    pil_image = Image.open('/home/xys/CloundShiProjects/traffic_light/trafficlight_classify/data/feijidongchedeng/8_516_9c4a497c-c019-48e8-b381-fd3fe247c7a1_1.jpg')
    array_image = preprocess(pil_image)
    array_image.show()
    array_image.save('/home/xys/CloundShiProjects/traffic_light/trafficlight_classify/test-筛选/0.jpg')


import cv2
import os
import numpy as np
def get_resize_square(input,output):#,output
    for filename in os.listdir(input):
        image_path=input+'/'+filename
        image=cv2.imread(image_path)
        x=image.shape[1]##宽度
        y=image.shape[0]##高度

        target = max(x,y)
        BLACK=[0, 0, 0]
        a = (target - x) / 2
        b = (target - y) / 2
        constant = cv2.copyMakeBorder(image, int(b), int(b), int(a), int(a), cv2.BORDER_CONSTANT, value=BLACK)
        return constant
if __name__=='__main__':
    input_image_dir=r'/home/xys/CloundShiProjects/traffic_light/trafficlight_classify/t'
    output_dir=r'/home/xys/CloundShiProjects/traffic_light/trafficlight_classify'
    get_resize_square(input_image_dir,output_dir)
